package com.lill.product.web;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.product.service.ISaleRoyaltyService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.model.SysUserEntity;

/**
 * 销售提成值Controller
 *
 * @author lill
 * @date 2021-09-06 20:23:17
 */
@RestController
@RequestMapping("base/saleroyalty")
public class BaseSaleRoyaltyController {
    @Autowired
    private ISaleRoyaltyService saleRoyaltyService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	params.put("companyId", user.getCompanyId());
    	if(StringUtils.isNotEmpty(user.getBranchId())){
    		params.put("branchId", user.getBranchId());
    	}else{
    		params.put("branchIdIsNull", user.getCompanyId());
    	}
        //查询列表数据
        Query query = new Query(params);

        List<Map<String, Object>> baseSaleRoyaltyList = saleRoyaltyService.queryList(query);
        int total = saleRoyaltyService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(baseSaleRoyaltyList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
    	Map<String, Object> royaltyMap = saleRoyaltyService.queryObject(id);
        return R.ok().put("productRoyalty", royaltyMap);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody Map<String, Object> productRoyalty,@CurrentUser SysUserEntity user) {
    	productRoyalty.put("company_id", user.getCompanyId());
    	productRoyalty.put("branch_id", user.getBranchId());
        int result=saleRoyaltyService.save(productRoyalty);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

}
